import json
from typing import List, Dict, Any
from .car_object import CarObject


class CarSeries:
    """
    车系实体类
    用于表示车辆系列，包含多个车款
    """
    
    def __init__(self, car_series_name: str, catalog_id: str = "", series_code: str = "", default_paint: str = ""):
        """
        初始化车系对象
        
        Args:
            car_series_name (str): 车系名，也是唯一值
            catalog_id (str, optional): 福特官网目录ID (如: WAEGB-CGE-2022-FocusMCAGBR202450)
            series_code (str, optional): 车系代码 (如: ACMAA_VS-D3)
            default_paint (str, optional): 默认颜色代码 (如: PN4A7)
        """
        self.car_series_name = car_series_name
        self.catalog_id = catalog_id
        self.series_code = series_code
        self.default_paint = default_paint
        self.car_object_list: List[CarObject] = []
    
    def add_car_object(self, car_object: CarObject):
        """
        添加车款对象
        
        Args:
            car_object (CarObject): 车款对象
        """
        if car_object and car_object not in self.car_object_list:
            # 确保车款的车系名称与当前车系一致
            car_object.series_name = self.car_series_name
            self.car_object_list.append(car_object)
    
    def remove_car_object(self, car_object: CarObject):
        """
        移除车款对象
        
        Args:
            car_object (CarObject): 要移除的车款对象
        """
        if car_object in self.car_object_list:
            self.car_object_list.remove(car_object)
    
    def get_car_object_by_model(self, model_name: str, drive: str = None) -> CarObject:
        """
        根据车型名称和驱动方式获取车款对象
        
        Args:
            model_name (str): 车型名称
            drive (str, optional): 驱动方式
            
        Returns:
            CarObject: 匹配的车款对象，如果未找到返回None
        """
        for car_object in self.car_object_list:
            if car_object.model_name == model_name:
                if drive is None or car_object.drive == drive:
                    return car_object
        return None
    
    def get_car_objects_count(self) -> int:
        """
        获取车款数量
        
        Returns:
            int: 车款数量
        """
        return len(self.car_object_list)
    
    def set_catalog_info(self, catalog_id: str, series_code: str, default_paint: str):
        """
        设置福特官网目录信息
        
        Args:
            catalog_id (str): 目录ID
            series_code (str): 车系代码
            default_paint (str): 默认颜色代码
        """
        self.catalog_id = catalog_id
        self.series_code = series_code
        self.default_paint = default_paint
    
    def get_catalog_info(self) -> Dict[str, str]:
        """
        获取福特官网目录信息
        
        Returns:
            Dict[str, str]: 包含目录信息的字典
        """
        return {
            "catalog_id": self.catalog_id,
            "series_code": self.series_code,
            "default_paint": self.default_paint
        }
    
    def toJSONString(self) -> str:
        """
        输出符合output.json格式的JSON字符串
        
        Returns:
            str: JSON格式的字符串
        """
        data = {
            "car_series_name": self.car_series_name,
            "catalog_id": self.catalog_id,
            "series_code": self.series_code,
            "default_paint": self.default_paint,
            "car_objects": [car_object.to_dict() for car_object in self.car_object_list]
        }
        return json.dumps(data, ensure_ascii=False, indent=4)
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            Dict[str, Any]: 字典格式的数据
        """
        return {
            "car_series_name": self.car_series_name,
            "catalog_id": self.catalog_id,
            "series_code": self.series_code,
            "default_paint": self.default_paint,
            "car_objects": [car_object.to_dict() for car_object in self.car_object_list]
        }
    
    def __str__(self) -> str:
        """字符串表示"""
        return f"CarSeries(name={self.car_series_name}, catalog_id={self.catalog_id}, objects_count={len(self.car_object_list)})"
    
    def __repr__(self) -> str:
        """详细字符串表示"""
        return (f"CarSeries(car_series_name='{self.car_series_name}', "
                f"catalog_id='{self.catalog_id}', series_code='{self.series_code}', "
                f"default_paint='{self.default_paint}')")
    
    def __eq__(self, other) -> bool:
        """相等性比较"""
        if not isinstance(other, CarSeries):
            return False
        return self.car_series_name == other.car_series_name
    
    def __hash__(self) -> int:
        """哈希值"""
        return hash(self.car_series_name)
